define(['ajaxErrorTips'], function(ajaxErrorTips){

	function getCloudDiskFolderInfo(callback,folderId){

		function getFolderNameById(data, id) {
			var folderName = '';
			for (var key in data) {
				if (data.hasOwnProperty(key)) {
					$.each(data[key], function (i, item) {
						if(item.id == id) {
							folderName = item.name
							return false;
						}
					})
				}
			}
			return folderName
		}

		if(!folderId){
			folderId=0;
		}

		ajaxMethod({
			url: '/api/cloud/folder/' + folderId,
			success: function (data, status) {
				if(status.code == 0) {

					var localFolderData = {
						type:	    'folder',
						id:			  data.folder_id,
						name:		  data.folder_name,
						pathId:	  '',
						pathStr:	data.folder_name,
						level:		''
					}
					callback(localFolderData);
				}
			}
		})

		/*$.ajax({
			dataType:'json',
			url: '/api/cloud',
			success:function(s,statusText){
				switch(resData.error_info.errno){
					case 1:
						var serverFolderData=resData.data;
						var localFolderData={
							type:		'folder',
							id:			serverFolderData.folder_id,
							name:		serverFolderData.folder_name,
							pathId:		serverFolderData.folder_path,
							pathStr:	serverFolderData.folder_path_str,
							level:		serverFolderData.operate_level,
							uploadTime:	serverFolderData.uptime
						}
						callback(localFolderData);
						break;
					default:
						ajaxErrorTips({
							titleText:'ServerError'.tr('common'),
							contentText:resData.error_info.error,errorNo:resData.error_info.errno
						});
				}
			},
			error:function(request,statusText,errorThrown){
				ajaxErrorTips({
					From:'getCloudDiskFolderInfo',
					StatusText:statusText,
					ErrorThrown:errorThrown,
					RequestStatus:request.status,
					RequestStatusText:request.statusText
				});
			},
			complete:function(request,statusText){}
		});*/
	}

	function getCloudDiskFolderSubList(callback,folderId,contentType){
		function ajaxSuccessCallback(data, status){
			switch(status.code){
				case 0://resData.error_info.error:程序正常执行
					var serverFolderList=data.folder_list;
					var localFolderList=[];
					$.each(serverFolderList, function(i,folderBaseData) {
						localFolderList.push({
							type:	'folder',
							id:		folderBaseData.folder_id,
							name:	folderBaseData.folder_name,
							empty:	!folderBaseData.hasSub
						})
					});
					callback(localFolderList);
					break;
				// case 106://resData.error_info.error:无数据
				// 	callback([]);
				// 	break;
				default:
					ajaxErrorTips({
						titleText:'ServerError'.tr('common'),
						contentText: status.message,
						errorNo: status.code
					});
			}
		}

		if(!folderId) {
			folderId = 0;
		}

		ajaxMethod({
			url: '/api/cloud/folder/' + folderId,
			success: function (data, status) {
				ajaxSuccessCallback(data, status)
			},
			error:function(request,statusText,errorThrown){
				ajaxErrorTips({
					From:'getCloudDiskfolderSubList',
					StatusText:statusText,
					ErrorThrown:errorThrown,
					RequestStatus:request.status,
					RequestStatusText:request.statusText
				});
			}
		})


/*		$.ajax({
			type:'post',
			dataType:'json',
			url:'/saasajax/cloud.ajax.php?action=getSubFolderList',
			data:{
				folderId:folderId
			},
			async:true,
			success:function(resData,statusText){
				//console.log(resData,JSON.stringify(resData));
				ajaxSuccessCallback(resData);
			},
			error:function(request,statusText,errorThrown){
				ajaxErrorTips({
					From:'getCloudDiskfolderSubList',
					StatusText:statusText,
					ErrorThrown:errorThrown,
					RequestStatus:request.status,
					RequestStatusText:request.statusText
				});
			},
			complete:function(request,statusText){
				//console.log(request,statusText);
			}
		});*/
	}

	function CloudDiskSelector(config){
		var selector=this;
		config=config || {};
		selector.$dom=$('<div class="cloudDiskSelector"></div>');
		/*function FileBox(){
			var fileBox=this;
			fileBox.$dom=$('<div class="fileBox"></div>');
			fileBox.$fileName=$('<div class="fileName"></div>').appendTo(fileBox.$dom);
			fileBox.$fileNameText=$('<div class="nameText"></div>').appendTo(fileBox.$fileName);
			folderBox.fileInitByData=function(fileData,callback){
				fileBox.fileId=fileData.id;
				fileBox.$fileNameText.text(fileData.fileName);
				callback();
			}
			fileBox.fileInitById=function(fileId,callback){
				getCloudDiskFolder(fileId,[],function(fileData){
					fileBox.fileInitByData(fileData,callback);
				});
			}
			fileBox.fileInit=function(fileInfo){
				switch(typeof fileInfo){
					case 'object':
						fileBox.initByFileData(fileInfo);
						break;
					default:
						fileBox.initByFileId(fileInfo)
				}
			}
		}*/
		function FolderBox(folderDeepLevel){
			var folderBox=this;
			folderBox.subListInited=false;
			folderBox.openStatus='close';
			folderDeepLevel=folderDeepLevel || 0;
			folderBox.$dom=$('<div class="folderBox"></div>');
			folderBox.$folder=$('<div class="folder"></div>').appendTo(folderBox.$dom);
			folderBox.$folder.css({
				'padding-left':folderDeepLevel*20+20
			});
			folderBox.$folderInfo=$('<div class="folderInfo"></div>').appendTo(folderBox.$folder);
			folderBox.$openStatusIcon=$('<span class="icon icon-size-16 icon-plus-square"></span>').appendTo(folderBox.$folderInfo);
			folderBox.$folderTypeIcon=$('<span class="icon icon-size-24 icon-clouddisk-folder"></span>').appendTo(folderBox.$folderInfo);


			folderBox.$folderNameText=$('<div class="nameText"></div>').appendTo(folderBox.$folderInfo);
			folderBox.$subListBox=$('<div class="subListBox"></div>').appendTo(folderBox.$dom);

			folderBox.folderInitByData=function(folderData,callback){

				folderBox.folderId=folderData.id;
				folderBox.folderData=folderData;
				folderBox.$folderNameText.text(folderData.name);
				if(typeof callback === 'function'){
					callback();
				}
			}
			folderBox.folderInitById=function(folderId,callback){
				getCloudDiskFolderInfo(function(folderInfoData){
					folderBox.folderInitByData(folderInfoData,callback);
				},folderId);
			}
			folderBox.folderInit=function(folderInfo,callback){
				switch(typeof folderInfo){
					case 'object':
						folderBox.folderInitByData(folderInfo,callback);
						break;
					default:
						folderBox.folderInitById(folderInfo,callback)
				}
			}


			function SubListDom(){
				var subListDom=this;
				subListDom.subList={};
				subListDom.$dom=$('<ul class="subList"></ul>');
				subListDom.addFolder=function(folderData){
					var subFolderBox=new FolderBox(folderDeepLevel+1);
					subFolderBox.folderInitByData(folderData);
					subFolderBox.$dom.appendTo(subListDom.$dom);
					subListDom.subList[folderData.id]=subFolderBox;
				}
				subListDom.clear=function(){
					subListDom.subList={};
					subListDom.$dom.html('');
				}
				subListDom.update=function(subListData){
					subListDom.clear();
					$.each(subListData, function(k,subData) {
						if(subData.type==='folder'){
							subListDom.addFolder(subData);
						}else{
							subListDom.addFile(subData);
						}
					});
				}
			}

			folderBox.subListInitByData=function(subListData,callback){
				folderBox.subListDom=new SubListDom();
				folderBox.subListDom.update(subListData);
				folderBox.$subListBox.html(folderBox.subListDom.$dom);
				callback();
			}
			folderBox.subListInitById=function(folderId,callback){
				if(folderBox.subListInited===false){
					folderBox.subListInited=true;
				}else{
					callback();
					return false;
				}

				getCloudDiskFolderSubList(function(folderSubList){
					folderBox.subListInitByData(folderSubList,callback);
				},folderId);
			}
			folderBox.openStyle=function(openStatus){
				if(openStatus){
					folderBox.$folder.removeClass('waitingOpen').addClass('open');
					folderBox.$subListBox.addClass('open');
					folderBox.$openStatusIcon.removeClass('icon-plus-square').addClass('icon-less-square');
					folderBox.openStatus='open';
				}else{
					folderBox.$folder.removeClass('open');
					folderBox.$subListBox.removeClass('open');
					folderBox.$openStatusIcon.addClass('icon-plus-square').removeClass('icon-less-square');
					folderBox.openStatus='close';
				}
			}
			folderBox.open=function(){
				folderBox.openStatus='waitingOpen';
				folderBox.$folder.addClass('waitingOpen');
				folderBox.subListInitById(folderBox.folderId,function(){
					folderBox.openStyle(true);
				});
			}
			folderBox.close=function(){
				folderBox.openStyle(false);
			}
			folderBox.check=function(){
				if(!folderBox.$folder.hasClass('checked')){
					selector.$dom.find('.folder').removeClass('checked');
					folderBox.$folder.addClass('checked');
					selector.checkedFolderData=$.extend(true, {}, folderBox.folderData);

				}
			}
			folderBox.$folder.on('click',function(){
				switch(folderBox.openStatus){
					case 'close':
						folderBox.open();
						break;
					case 'open':
						folderBox.close();
						break;
				}
				folderBox.check();
			});
		}
		selector.rootFolderId=0;
		if(config.rootFolderId){
			selector.rootFolderId=config.rootFolderId;
		}
		selector.rootFolderBox=new FolderBox();
		selector.rootFolderBox.$dom.appendTo(selector.$dom);





		if(config.usePopUpBox!==false){
			selector.popUpBox=new PopUpBox({
				'type':'dom',
				'style':'default autoResize',
				'submitBtnType':'alert',
				'closeClear':false,
				'titleText':config.title || '选择资源',
				'content':selector.$dom,
				'callback':{
					'yes':function(e){
						if(selector.checkedFolderData===undefined){
							_Tips('请选择一个文件夹');
							return false;
						}
						//console.log($(e.content).find('li.checked').attr('data-courseid'));
					}
				},
				'position':{
					width:640,
					top:80,
					bottom:80
				}
			});
			selector.popUpBox.submitBtnBox.$dom.css('text-align','right');
    		selector.$bottomLeft = $('<div style="z-index: 1; position: absolute; height: 55px; left: 0; bottom: -55px; padding: 10px; line-height: 32px;"></div>').appendTo(selector.popUpBox.$content);
		}

	}

	window.getCloudDiskFolderInfo = getCloudDiskFolderInfo;
	window.getCloudDiskFolderSubList = getCloudDiskFolderSubList;
	window.CloudDiskSelector=CloudDiskSelector;

	return CloudDiskSelector;
})
